/*
 *  Copyright (C) 1993  Robert Davis
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of Version 2, or any later version, of 
 *  the GNU General Public License as published by the Free Software 
 *  Foundation.
 */

static char RCSId[]="$Id: Preferences.m,v 1.7 1993/05/18 03:55:36 davis Exp $";


#import <appkit/Application.h>
#import <appkit/Button.h>
#import <appkit/Listener.h>
#import <appkit/Matrix.h>
#import <appkit/Panel.h>

#import <defaults/defaults.h>

#import "Gnuplot.h"
#import "Preferences.h"

#define	OWNER	"Gnuplot"		/* Owner of defaults	*/

static NXDefaultsVector GnuplotDefaults = {
    {"ConstantUpdate", "1"},		/* Update after every change	*/
    {"NewDocument", "1"},		/* Create a new doc at launch	*/
    {"DefaultInspector", "2"},		/* Default Pane is Data		*/
    {"ReadInitFile", "1"},		/* Currently unused		*/
    {"HalvePlot", "1"},			/* Make plots half-size		*/
    {NULL}
};


@implementation Preferences

- init
{
    [super init];
    NXRegisterDefaults(OWNER, GnuplotDefaults);

    /*  Set instance variables to their defaults.  */

    constantUpdate = *NXGetDefaultValue(OWNER, "ConstantUpdate") - '0';
    [Gnuplot setConstantUpdate:constantUpdate];

    newDocument = (*NXGetDefaultValue(OWNER, "NewDocument") != '0');
    defaultInspector = *NXGetDefaultValue(OWNER, "DefaultInspector") - '0';

    halvePlot = (*NXGetDefaultValue(OWNER, "HalvePlot") != '0');
    [Gnuplot setHalvePlot:halvePlot];

    return self;
}


- free
{
    [window free];
    return [super free];
}


- awakeFromNib
{
    [window setFrameUsingName:"PreferencesPanel"];
    [window setFrameAutosaveName:"PreferencesPanel"];

    [constantUpdateMatrix selectCellWithTag:constantUpdate];

    [newDocumentButton setState:newDocument];
    [defaultInspectorMatrix selectCellWithTag:defaultInspector];
    [halvePlotMatrix selectCellWithTag:halvePlot? 1:0];

    return self;
}


- showPanel:sender
{
    if (!window) {
	[NXApp loadNibSection: "Preferences.nib" 
			owner: self
		    withNames: NO
		     fromZone: [self zone]];
    }

    [window makeKeyAndOrderFront:self];
    return self;
}



- doSetConstantUpdate:sender
{
    char aString[2];

    constantUpdate = [constantUpdateMatrix selectedTag];
    aString[0] = '0' + constantUpdate;
    aString[1] = '\0';
    NXWriteDefault (OWNER, "ConstantUpdate", aString);

    [Gnuplot setConstantUpdate:constantUpdate];
    return self;
}



- (int)constantUpdate
{
    return constantUpdate;
}


- doSetNewDocument:sender
{
    const char *aString;

    if (newDocument = [newDocumentButton state]) aString = "1";
    else aString = "0";
    NXWriteDefault (OWNER, "NewDocument", aString);

    return self;
}



- (BOOL) newDocument
{
    return newDocument;
}


- doSetDefaultInspector:sender
{
    char aString[2];

    defaultInspector = [defaultInspectorMatrix selectedTag];
    aString[0] = '0' + defaultInspector;
    aString[1] = '\0';
    NXWriteDefault (OWNER, "DefaultInspector", aString);
    return self;
}


- (int)defaultInspector
{
    return defaultInspector;
}


- doSetHalvePlot:sender
{
    const char *aString;

    if (halvePlot = [halvePlotMatrix selectedTag]) aString = "1";
    else aString = "0";
    NXWriteDefault (OWNER, "HalvePlot", aString);

    [Gnuplot setHalvePlot:halvePlot];

    return self;
}


- (BOOL)halvePlot
{
    return halvePlot;
}



// Shuts up the compiler about unused RCSId
- (const char *) rcsid
{
    return RCSId;
}


@end
